package com.lwy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwy.dao.BookNovelChapterMapper;
import com.lwy.entity.BookMessage;
import com.lwy.entity.BookNove;
import com.lwy.entity.BookNovelChapter;
import com.lwy.entity.BookShelf;
import com.lwy.service.BookNovelChapterService;
import com.lwy.util.ChapterFilter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//章节表service接口实现类
@Service
public class BookNovelChapterServiceImpl extends ServiceImpl<BookNovelChapterMapper, BookNovelChapter> implements BookNovelChapterService {


    @Resource
    private BookShelfServiceImpl bookShelfService;
    @Resource
    private BookMessageServiceImpl bookMessageService;
    @Resource
    private BookNoveServiceImpl bookNoveService;

    @Resource
    private BookNovelChapterServiceImpl bookNovelChapterService;



    public List<BookNovelChapter> listAll(Integer novel_id){
        List<BookNovelChapter> bookNovelChapters = baseMapper.selectList(new QueryWrapper<BookNovelChapter>().eq("novel_id",novel_id));
        for (BookNovelChapter bookNovelChapter : bookNovelChapters) {
            ChapterFilter.setEntity(bookNovelChapter);
        }
        return bookNovelChapters;
    }

    public long updWord(String url,String word){
        long word1 = ChapterFilter.getWord(url);
        long aLong = Long.valueOf(word);
        long l = aLong + word1;
        return l;
    }


    public PageInfo<BookNovelChapter> page(String m,Integer novel_id,Integer pageNum, Integer pageSize){
        BookNovelChapter bookNovelChapter1 = baseMapper.newList(novel_id);
        if(null==pageNum){
            pageNum=1;
        }
        if(m.equals("这一章")){

        }else if(m.equals("上一章")){
            pageNum=pageNum-1;
        }else{
            if(bookNovelChapter1.getCid()==pageNum){
                pageNum=pageNum;
            }else{
                pageNum=pageNum+1;
            }

        }

        if(null==pageSize){
            pageSize=1;
        }
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<BookNovelChapter> pageInfo = new PageInfo<>(baseMapper.selectList(new QueryWrapper<BookNovelChapter>().eq("novel_id", novel_id)));
        List<BookNovelChapter> list = pageInfo.getList();
        for (BookNovelChapter bookNovelChapter : list) {
            ChapterFilter.setEntity(bookNovelChapter);
        }
        return  pageInfo;
    }

    public BookNovelChapter newList(Integer novel_id){
        BookNovelChapter bookNovelChapter =baseMapper.newList(novel_id);
        if(null==bookNovelChapter){
            BookNovelChapter bookNovelChapter1 = new BookNovelChapter();
            bookNovelChapter1.setTitle("暂无章节更新");
            return bookNovelChapter1;
        }else{
            ChapterFilter.setEntity(bookNovelChapter);
            return bookNovelChapter;
        }
    }

    public Integer title(String title,Integer novel_id){
        StringBuffer sb = new StringBuffer();
        List<BookNovelChapter> bookNovelChapters = baseMapper.selectList(new QueryWrapper<BookNovelChapter>().eq("novel_id",novel_id));
        if(bookNovelChapters.size()>0){
            for (BookNovelChapter novelChapter : bookNovelChapters) {
                sb.append(ChapterFilter.getTile(novelChapter.getReurl()));
            }

        }
        String string = sb.toString();
        if(string.contains(title)){
            return 1;
        }else{
            return 2;
        }
    }

    public String getDate(){
        Date date = new Date();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        return  sd.format(date);
    }

    public Integer saveChapter(String title, String chapter,Integer novel_id) {
        String url = ChapterFilter.getUrl(title, chapter);
        BookNove id = bookNoveService.getOne(new QueryWrapper<BookNove>().eq("id", novel_id));
        String word1 = id.getWord();
        long l = updWord(url, word1);
        String str = String.valueOf(l);
        BookNove bookNove = new BookNove();
        bookNove.setId(novel_id);
        bookNove.setWord(str);
        bookNoveService.updateById(bookNove);
        int novel_id1 = bookNovelChapterService.count(new QueryWrapper<BookNovelChapter>().eq("novel_id", novel_id));
        BookNovelChapter bookNovelChapter = new BookNovelChapter();
        bookNovelChapter.setNovel_id(novel_id);
        bookNovelChapter.setReurl(url);
        bookNovelChapter.setCid(novel_id1+1);
        Integer title1 = title(title,novel_id);
        if(title1==1){
            return 1;
        }else{
            int insert = baseMapper.insert(bookNovelChapter);
            List<BookShelf> list = bookShelfService.list(new QueryWrapper<BookShelf>().eq("novel_id", novel_id).eq("status", 1));
            if(list.size()>0){
                for (BookShelf bookShelf : list) {
                    Integer user_id = bookShelf.getUser_id();
                    BookMessage bookMessage = new BookMessage();
                    bookMessage.setUid(user_id);
                    bookMessage.setChapter_id(bookNovelChapter.getId());
                    bookMessage.setContent("您观看的图书更新啦~");
                    bookMessage.setNovel_id(bookShelf.getNovel_id());
                    bookMessage.setStatus(1);
                    bookMessage.setType("1");
                    bookMessage.setCreate_time(getDate());
                    boolean save = bookMessageService.save(bookMessage);
                    if(save){
                        insert+=1;
                    }
                }
            }

            if(insert>0){
                return 2;
            }else{
                return 3;
            }
        }

    }

    public List<BookNovelChapter> firstList(Integer novel_id){
        return baseMapper.firstList(novel_id);
    }

}
